﻿using Autofac;
using Autofac.Extras.DynamicProxy;
using MyExtension.AOP;
using MyRepository.BASE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace MyExtension.ServiceExtensions
{
    /// <summary>
    /// Autofac 服务注册
    /// </summary>
    public class AutofacModuleRegister : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            // TODO AOP注入未完成
            List<Type> types = new List<Type>();

            //builder.RegisterType<LogAop>();
            builder.RegisterType<MyCacheAop>();
            //types.Add(typeof(LogAop));
            types.Add(typeof(MyCacheAop));
            // 只注册仓储也可以？ 我之前都注册了 泛型仓储和泛型服务 这里只注册泛型仓储 也可以了。
            builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IBaseRepository<>)).InstancePerDependency(); // 注册仓储

            //注册服务
            var assemblyService = Assembly.Load("MyServices");
            builder.RegisterAssemblyTypes(assemblyService)
                .AsImplementedInterfaces()
                .InstancePerDependency()
                .PropertiesAutowired()
                .EnableInterfaceInterceptors()
                .InterceptedBy(types.ToArray());

            // 注册仓储
            var assemblyRepo = Assembly.Load("MyRepository");
            builder.RegisterAssemblyTypes(assemblyRepo)
                .AsImplementedInterfaces()
                .InstancePerDependency()
                .PropertiesAutowired();

            
        }
    }
}
